"""
Case Type   : 资源池化集群支持多链路
Case Name   : switchover过程中断开升主节点全链路
Create At   : 2025/04/13
Owner       : @kyleze
Description :
    1.查询集群状态
    2.执行switchover,并且再断开全链路
    3.获取switchover结果
    4.恢复链路
    5.恢复集群主备关系
Expect      :
    1.数据库状态正常
    2.成功
    3.失败
    4.成功
    5.成功
History     :
"""
import os
import unittest
import time

from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.ComThread import ComThread

from yat.test import Node
from yat.test import macro


class MultipathTest(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.com = Common()
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.pri_user_node = Node('PrimaryDbUser')
        self.pri_root = Node(node="PrimaryRoot")
        self.sta1_sh = CommonSH('Standby1DbUser')
        self.sta1_root = Node(node="Standby1Root")
        self.ip_1 = macro.STANDBY1_LUN_IP.split()[0].strip()
        self.ip_2 = macro.STANDBY1_LUN_IP.split()[1].strip()
        self.log.info("----获取备1节点网络设备名称----")
        self.st1_network_card = []
        interfaces = self.sta1_root.sh("ip -o addr show").result()
        for item in interfaces.split('\n'):
            if self.ip_1 in item or self.ip_2 in item:
                self.st1_network_card.append(item.split(' ')[1].split(':')[0])
        self.log.info(self.st1_network_card)

    def test_multipath(self):
        step = '--step1:查询集群状态 expect:成功--'
        self.log.info(step)
        cluster_status = self.primary_sh.get_db_cluster_status('status')
        self.log.info(cluster_status)
        self.assertTrue(cluster_status, "执行失败" + step)

        step = '--step2:执行switchover,并且再断开全链路 expect:成功--'
        self.log.info(step)
        switchover_thread = \
            ComThread(self.sta1_sh.exec_cm_ctl,
                      args=('switchover', f'-n 2 -D {macro.DB_INSTANCE_PATH}'))
        switchover_thread.setDaemon(True)
        switchover_thread.start()
        time.sleep(2)
        res = self.sta1_root.sh(f'ifdown {self.st1_network_card[0]};'
                                f'ifdown {self.st1_network_card[1]}').result()
        self.log.info(res)
        self.assertEqual(
            2, res.count("successfully deactivated"), "执行失败" + step)
        switchover_thread.join(120)

        step = '--step3:获取switchover结果 expect:失败--'
        self.log.info(step)
        switchover_res = switchover_thread.get_result()
        self.log.info(switchover_res)
        switchover_bool = switchover_res is None or \
            self.constant.cm_switchover_success_msg not in switchover_res
        self.assertTrue(switchover_bool, "执行失败" + step)

        step = '--step4:恢复链路 expect:成功--'
        self.log.info(step)
        res = self.sta1_root.sh(f'ifup {self.st1_network_card[0]};'
                                f'ifup {self.st1_network_card[1]}').result()
        self.log.info(res)
        self.assertEqual(2, res.count("successfully activated"), "执行失败" + step)

        step = '--step6:恢复集群 expect:成功--'
        self.log.info(step)
        start_res = self.primary_sh.exec_cm_ctl("start", '-n 2')
        self.log.info(start_res)
        self.assertIn(self.constant.cm_start_node_success_msg, start_res,
                      '执行失败' + step)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end----')

